/* -*- mode: c -*-
 * Copyright (C) 2010, 2011 Lluís Vilanova <vilanova@ac.upc.edu>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#ifndef TF_MM_CONS_H
#define TF_MM_CONS_H

#include <stdint.h>

#ifdef __cplusplus
extern "C" {
#endif


/** Try performing a shared memory acquire.
 *
 * This should be used for read-only data with multiple readers.
 *
 * The operation will succeed if the bytes to acquire are either shared acquired
 * or released.
 *
 * @note Due to a simplified implementation, if the memory is already acquired,
 *       this will only succeed if it was acquired with the same address and
 *       size.
 *
 * @pre The memory is not already acquired by the same core (i.e., double
 *      acquires not allowed) [debug]
 *
 * @return First byte address that could not be acquired; NULL otherwise.
 */
static void * tf_mm_cons_try_shared_acquire (void * base, size_t bytes);

/** Perform a shared memory acquire.
 *
 * Loops until @ref tf_mm_cons_try_shared_acquire is successful.
 */
static void tf_mm_cons_shared_acquire (void * base, size_t bytes);


/** Try upgrading a shared memory acquire into an exclusive acquire.
 *
 * @pre The bytes to acquire must have been previously shared acquired.
 *
 * @pre The memory was already acquired by the same core [debug]
 *
 * @note @ref base is the first byte of a previously acquired range.
 *
 * @return Wether the upgrade was successful.
 */
static unsigned int tf_mm_cons_try_upgrade_acquire (void * base);

/** Perform an upgrade from a shared memory acquire into an exclusive acquire.
 *
 * Loops until @ref tf_mm_cons_try_upgrade_acquire is successful.
 *
 * @warning Two subjects performing an upgrade on the same memory will result in
 *          a deadlock, as the memory has been previously acquired as shared by
 *          both of them.
 */
static void tf_mm_cons_upgrade_acquire (void * base);


/** Exclusive memory acquire.
 *
 * This should be used for writing data.
 *
 * The operation will succeed if the bytes to acquire are released.
 *
 * @note Due to a simplified implementation, if the memory is already acquired,
 *       this will only succeed if it was acquired with the same address and
 *       size.
 *
 * @pre The memory is not already acquired by the same core (i.e., double
 *      acquires not allowed) [debug]
 *
 * @return First byte address that could not be acquired; NULL otherwise
 */
static void * tf_mm_cons_try_exclusive_acquire (void * base, size_t bytes);

/** Exclusive memory acquire.
 * Loops until @ref tf_mm_cons_try_exclusive_acquire is successful.
 */
static void tf_mm_cons_exclusive_acquire (void * base, size_t bytes);


/** Release memory.
 *
 * The operation will succeed if the bytes to release are acquired.
 *
 * @pre @ref base is the first byte of a previously acquired range.
 *
 * @return Wether the release was successful.
 */
static unsigned int tf_mm_cons_release (void * base);

#define TF_MM_CONS_INTERNAL_H
#include "tf_mm_cons_internal.h"
#undef TF_MM_CONS_INTERNAL_H


#ifdef __cplusplus
}
#endif

#endif  /* TF_MM_CONS_H */
